// @flow
// import $ from 'jquery'
import React from 'react'
import moment from 'moment'
import styles from './HistoryLine.css'
import { Modal, Tabs, Tag } from 'antd'
// import Highcharts from 'highcharts-release'
import HighStock from 'highcharts-release/highstock.js'
import HighchartsCustomEvents from 'highcharts-custom-events'
import basicContext from 'basiccontext'
import 'basiccontext/dist/basicContext.min.css'
import 'basiccontext/dist/themes/bright.min.css'
import 'basiccontext/dist/addons/popin.min.css'
import HistoryPolar from 'components/HistoryPolar'
import HistoryPolarAxis from 'components/HistoryPolarAxis'
import Spectrum from 'components/Spectrum'
import Bode from 'components/Bode'
import AddChartEvent from 'components/AddChartEvent'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import R from 'ramda'
import ReactDom from 'react-dom'
import { connect } from 'react-redux'
import { changeHistoryChartCond,
  changeDynamicChartCond, changeDynamicDateCond } from 'store/modules/deviceFileCondition'
const TabPane = Tabs.TabPane
// import theme from './theme.js'
HighchartsCustomEvents(HighStock)
type Props = {
  // xData: Array<any>,
  // historyFrom: number,
  // historyTo: number,
  chartData: Array<any>,
  title: String,
  yTitle: String,
  showContextMenu?: boolean,
  type?: string,
  deviceCard: Object,
  updateEvents: Function,
  deviceFileCondition: Object,
  userId: string
}

type States = {
  spectrumVisible: boolean,
  visible: boolean,
  checkTime: any,
  checkName: string,
  activeKey: string,
  channel_id: number
}
let innerChart = {}
class HistoryLine extends React.Component {
  props: Props
  state: States
  chart: Object
  addEvents: Function
  showChart: Function
  closeModal: Function
  removeEventsModal: Function
  changeTab: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      spectrumVisible: false,
      visible: false,
      checkTime: '',
      checkName: '',
      activeKey: 'spectrum',
      channel_id: 0
    }
    this.addEvents = this.addEvents.bind(this)
    this.showChart = this.showChart.bind(this)
    this.changeTab = this.changeTab.bind(this)
    this.closeModal = this.closeModal.bind(this)
    this.removeEventsModal = this.removeEventsModal.bind(this)
  }

  componentDidMount () {
    this.draw(this.props)
  }
  componentWillReceiveProps (nextProps: Props) {
    if (!R.equals(nextProps, this.props)) {
      this.draw(nextProps)
    }
  }

  addEvents (e: Object) {
    const { x, series } = e.point
    const { deviceCard, updateEvents, userId } = this.props
    const container = this.refs.chartEvent
    universalFetch(`${__SERVICE_API__}sys/users/${userId}`)
      .then(res => res.json())
      .then((json) => {
        const userName = json.result.name
        ReactDom.render(
          <AddChartEvent {...{
            userName,
            eventTime: x,
            deviceCard,
            updateEvents,
            removeEventsModal: this.removeEventsModal,
            channel_id: series.options.compoundId.channel_id,
            variableNameEn: series.options.field,
            sensorNameEn: series.options.sensorNameEn,
            sensorName: series.options.sensorName,
            checkName: series.name
          }} />,
          container
        )
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }

  removeEventsModal () {
    const container = this.refs.chartEvent
    ReactDom.unmountComponentAtNode(container)
  }
  closeModal () {
    this.setState({
      visible: false
    })
  }
  showChart (e: Object, active) {
    const { x, series } = e.point
    // series.name.split('__')[1]
    this.setState({
      visible: true,
      checkTime: x,
      channel_id: series.options.compoundId.channel_id,
      checkName: series.name, // '通频值__YT111A'
      activeKey: active
    })
  }
  changeTab (key: string) {
    this.setState({
      activeKey: key
    })
  }
  draw (props: Props) {
    /**
     * 为了让多个图表的提示框即十字准星线能够联动，这里绑定多个图表的附件 dom 的鼠标事件进行联动
     * (HighStock.charts.length - 2) 仅循环最后两个
     */
    // $('#container').bind('mousemove touchmove touchstart', function (e) {
    //   var chart,
    //     point,
    //     i,
    //     event
    //   for (i = 0; i < HighStock.charts.length; i = i + 1) {
    //     chart = HighStock.charts[i]
    //     if (chart) {
    //       event = chart.pointer.normalize(e.originalEvent)
    //       // 多个charts 注意循环
    //       // for (var ii = 0; ii < chart.series.length - 1; ii = ii + 1) {
    //         // point = chart.series[ii].searchPoint(event, true) // Get the hovered point
    //         // if (point) {
    //         //   point.highlight(e)
    //         // }
    //       // }
    //       var pointArr = []
    //       for (var ii = 0; ii < chart.series.length - 1; ii = ii + 1) {
    //         point = chart.series[ii].searchPoint(event, true) // Get the hovered point
    //         pointArr.push(chart.series[ii].searchPoint(event, true)) // Get the hovered point
    //         if (point) {
    //           point.highlight(e, pointArr)
    //         }
    //       }
    //     }
    //   }
    // })
    /**
     * 重写内部的方法， 没有鼠标划入操作后提示框隐藏关闭
     */
    // HighStock.Pointer.prototype.reset = function () {
    //   return undefined
    // }
    /**
     * 高亮当前的数据点，并设置鼠标滑动状态及绘制十字准星线
     */
    // HighStock.Point.prototype.highlight = function (event, chart) {
    //   this.onMouseOver() // 显示鼠标激活标识
    //   this.series.chart.xAxis[0].drawCrosshair(event, this) // 显示十字准星线
    //   this.series.chart.tooltip.refresh(chart) // 显示提示框
    // }
    /**
     * 同步缩放效果，即当一个图表进行了缩放效果，其他图表也进行缩放
     */
    function syncExtremes (e) {
      var thisChart = this.chart
      if (e.trigger !== 'syncExtremes') {
        HighStock.each(HighStock.charts, function (chart) {
          if (chart && chart !== thisChart) {
            if (chart.xAxis && chart.xAxis[0].setExtremes) {
              chart.xAxis[0].setExtremes(e.min, e.max, undefined, false, { trigger: 'syncExtremes' })
            }
          }
        })
      }
    }

    const that = this
    // const container = this.refs.container
    // const { type } = this.props
    props.chartData.map((chartItem, iii) => {
      // y轴的最小值（如果都是正数，最小值为0，有负数就不设置最小值）
      let minValue = 0
      chartItem.map((d, i) => {
        d.data.map((dd, ii) => {
          if (dd[1] < 0) {
            minValue = null
          }
        })
      })
      // 相位的值小于0后就讓其值加360°
      if (chartItem[0].field === 'p1x') { // 相位
        chartItem[0].data.map((d, i) => {
          if (d[1] < 0) {
            d[1] = d[1] + 360
          }
        })
      }

      let container = this.refs.chart1
      if (iii === 1) {
        container = this.refs.chart2
      }
      HighStock.setOptions({ global: { useUTC: false } })
      innerChart = HighStock.StockChart(container, {
        chart: {
          backgroundColor: 'transparent',
          zoomType: 'x',
          margin: [40, 190, 30, 60]
        },
        rangeSelector : {
          enabled: false,
          buttons : [{
            type : 'all',
            count : 1,
            text : 'All'
          }],
          selected : 1,
          inputEnabled : false
        },
        title: {
          text: chartItem[0].Title,
          x: -20,
          style: {
            fontSize: 12
          }
        },
        credits:{
          enabled: false
        },
        legend: {
          enabled: false,
          verticalAlign: 'top'
        },
        tooltip: {
          // positioner: function () {
          //   return {
          //     x: this.chart.chartWidth - this.label.width, // right aligned
          //     y: -1 // align to title
          //   }
          // },
          // borderWidth: 0,
          // backgroundColor: 'none',
          // headerFormat: '',
          shadow: false,
          shared: true,
          useHTML: true,
          formatter: function () {
            let t = '<p style="text-align: left"><b>' + moment(this.x, 'x').format('YYYY-MM-DD HH:mm:ss') + '</b></p>'
            this.points.map(function (d, i) {
              const y = d.y.toFixed(3) * 1000 / 1000
              t += '<p style="text-align: left"><span style="color: ' + d.color + '">●</span><b>' + d.series.name + '：' + y + '</b></p>'
            })
            return t
          },
          valueDecimals: 2
        },
        xAxis: {
          type: 'datetime',
          events: {
            setExtremes: syncExtremes
          },
          dateTimeLabelFormats: {
            second: '%Y-%m-%d<br>%H:%M:%S',
            minute: '%Y-%m-%d<br>%H:%M',
            hour: '%Y-%m-%d<br>%H:%M',
            day: '%Y-%m-%d',
            week: '%Y-%m-%d',
            month: '%Y-%m',
            year: '%Y'
          },
          plotBands: [{
            color: 'rgba(68, 170, 213, .2)'
          }]
        },
        yAxis: {
          opposite: false,
          lineWidth: 1,
          min: minValue,
          title: {
            align: 'high',
            offset: 0,
            rotation: 0,
            y: -10,
            text: props.yTitle
          },
          plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
          }]
        },
        navigator: {
          height: 30,
          yAxis: {
            min: minValue
          }
        },
        plotOptions: {
          line: {
            lineWidth: 1,
            dataGrouping: {
              enabled: false
            }
          },
          series: {
            animation: true,
            allowPointSelect: true,
            cursor: 'pointer',
            point: {
              allowPointSelect: true,
              cursor: 'pointer',
              events: {
                select: function () {
                  // const chart = this.series.chart
                  HighStock.charts.map((d, i) => {
                    if (d && d.renderTo.title === '历史数据') {
                      const xName = moment(this.category, 'x').format('YYYY-MM-DD HH:mm:ss')
                      const x = this.category
                      let yText = ''
                      // d.series.map((dd, ii) => {
                      //   const yData = d.series[ii].data.filter((dd) => dd.category === x)
                      //   if (yData.length === 0) return
                      //   const y = yData[0].y
                      //   yText += '<span style="color:' + d.series[ii].color + ';margin-right:5px">●</span>' +
                      //   d.series[ii].name + ':' + y.toFixed(3) * 1000 / 1000 + '<br/>'
                      // })
                      for (var ii = 0; ii < d.series.length - 1; ii++) {
                        const yData = d.series[ii].data.filter((dd) => dd.category === x)
                        if (yData.length === 0) return
                        const y = yData[0].y
                        yText += '<span style="color:' + d.series[ii].color + ';margin-right:5px">●</span>' +
                        d.series[ii].name + ':' + y.toFixed(3) * 1000 / 1000 + '<br/>'
                      }
                      const text = xName + '<br/> ' + yText
                      d.xAxis[0].removePlotLine('plot-line-1')
                      d.xAxis[0].addPlotLine({
                        value: this.category,
                        color: '#000',
                        width: 0.5,
                        id: 'plot-line-1'
                      })
                      if (!d.lbl) {
                        d.lbl = d.renderer.label(text, d.chartWidth - d.marginRight, 0)
                          .attr({
                            padding: 5,
                            r: 5,
                            fill: '#fff',
                            zIndex: 5
                          })
                        .css({
                          color: '#000'
                        })
                          .add()
                      } else {
                        d.lbl.attr({
                          text: text
                        })
                      }
                    }
                  })
                },
                contextmenu: (e) => {
                  if (!this.props.showContextMenu) {
                    const contextMenu = [
                      {
                        title: '添加事件',
                        icon: 'fa fa-plus',
                        fn: () => {
                          that.addEvents(e)
                        }
                      },
                      {
                        title: '波形频谱图',
                        icon: 'fa fa-line-chart',
                        fn: () => {
                          that.showChart(e, 'spectrum')
                        }
                      },
                      {
                        title: '原始轴心轨迹图',
                        icon: 'fa fa-eercast',
                        fn: () => {
                          that.showChart(e, 'polarAxis')
                        }
                      },
                      {
                        title: '合成轴心轨迹图',
                        icon: 'fa fa-bullseye',
                        fn: () => {
                          that.showChart(e, 'polar')
                        }
                      },
                      {
                        title: '伯德图',
                        icon: 'fa fa-area-chart',
                        fn: () => {
                          that.showChart(e, 'bode')
                        }
                      }
                    ]

                    basicContext.show(contextMenu, e)
                  }
                }
              }
            }
          }
        },
        series: chartItem
      })
      setTimeout(() => {
        innerChart.reflow()
      }, 5)
    })
  }

  render () {
    const { visible, checkTime, activeKey, checkName, channel_id } = this.state
    const { type, deviceCard, deviceFileCondition } = this.props
    const title = deviceCard.deviceData.customerName + '/' + deviceCard.deviceData.name
    const operations = <Tag style={{ marginRight: 20, cursor: 'default' }}>设备：{title}</Tag>
    return (
      <div>
        <div ref='container' id='container' className={styles['container']} >
          <div ref='chart1' title='历史数据' className={styles['chart']} />
          <div ref='chart2' title='历史数据' className={styles['chart']} />
        </div>
        <div ref='chartEvent' />
        <Modal
          width={1000}
          style={{ top: '5%' }}
          maskClosable={false}
          visible={visible}
          onCancel={this.closeModal}
          footer={null}
          header={null}
          >
          <div>
            <Tabs tabBarExtraContent={operations} activeKey={activeKey} size='small' onChange={this.changeTab}>
              <TabPane tab='波形频谱图' key='spectrum'>
                <Spectrum {...{
                  type,
                  historyCond: deviceFileCondition.historyCond,
                  deviceData: deviceCard.deviceData,
                  checkTime,
                  checkName,
                  channel_id
                }} />
              </TabPane>
              <TabPane tab='原始轴心轨迹图' key='polarAxis'>
                <HistoryPolarAxis {...{
                  historyCond: deviceFileCondition.historyCond,
                  deviceData: deviceCard.deviceData,
                  channel_id,
                  checkTime
                }} />
              </TabPane>
              <TabPane tab='合成轴心轨迹图' key='polar'>
                <HistoryPolar {...{
                  historyCond: deviceFileCondition.historyCond,
                  deviceData: deviceCard.deviceData,
                  channel_id,
                  checkTime
                }} />
              </TabPane>
              <TabPane tab='伯德图' key='bode'>
                <Bode {...{
                  historyCond: deviceFileCondition.historyCond,
                  deviceData: deviceCard.deviceData,
                  channel_id
                }} />
              </TabPane>
            </Tabs>
          </div>
        </Modal>
      </div>
    )
  }
}

const mapStatesToProps = (state) => {
  return {
    deviceCard: state.deviceCard,
    deviceFileCondition: state.deviceFileCondition,
    userId: state.user.userId
  }
}
export default connect(mapStatesToProps, {
  changeHistoryChartCond, changeDynamicChartCond, changeDynamicDateCond })(HistoryLine)
